#include "automata2.h"
#include "testInputs.h"


void testInput(t_table * t)
{
     int i,j, result;
     void * aux;
     
     printf("Inputs: %d\n\n", t->input.dim);
     for (i = 0; i < t->input.dim; i++)
        printf("  Input chain %d: \"%s\".\n", i, t->input.chains[i]);
     
     for(i=0;i<t->input.dim;i++)
     {
          result=validarInput(t,t->input.chains[i]);
          
          printf("chains[%d]: %p. string: %s\n", i, t->input.chains[i], t->input.chains[i]);
          
          printf("aux: %p. chains[%d]: %p. string: %s\n", aux, i, t->input.chains[i], t->input.chains[i]);
          //Reserves some space in case empty chain was entered


          if((aux=(char*)realloc(t->input.chains[i],4))==NULL)
             error(1,"Could not allocate memory. Shame on you.");
          else
             t->input.chains[i]=aux;

          printf("aux: %p. chains[%d]: %p. string: %s\n\n", aux, i,t->input.chains[i], t->input.chains[i]);


         
          //Replace input chain with result
          if (result)
          {
//             t->input.chains[i] = "SI";
             strcpy(t->input.chains[i],"SI");
//             t->input.chains[i][0] = 'S';
//             t->input.chains[i][1] = 'I';
//             t->input.chains[i][2] = 0;
        }
          else
          {
//             t->input.chains[i] = "NO";
//             t->input.chains[i][0] = 'N';
//             t->input.chains[i][1] = 'O';
//             t->input.chains[i][2] = 0;
             strcpy(t->input.chains[i],"NO");   
        }
        
   
    //    if (i)
      //    printf(" Prev (%p)-> %s\n", t->input.chains[i-1], t->input.chains[i-1]);
          
          printf("salida (in)[i:%d][p:%p][%s] \n",i, t->input.chains[i], t->input.chains[i]);

     }

//          printf("salida (in)[i:%d][p:%p][%s] \n",i, t->input.chains[i], t->input.chains[i]);


     for(i=0;i<t->input.dim;i++)
     {
          printf("salida (out)[i:%d][p:%p][%s] \n",i, t->input.chains[i], t->input.chains[i]);
     }

}

int validarInput(t_table * t,char * string)
{
    int state,j=0,col;
    
    state=t->iniState;    
    //    printf("---Cadena: \"%s\".\n Length: %d.\n", string, strlen(string));
    
    printf("ENTER validarInput\n");
    
    while (string[j])
    {
        putchar('#');
        col=getSymIndex(t->alphabet,string[j]);
        state=t->transitions.data[state][col];
        if (state==-1)
        {
            printf("Estado trampa!!\n");
            return 0;
	    }
        
        j++;
    }


    printf("LEAVING validarInput\n");
    
    if (foundinFinalStates(t->finalStates,t->states.vec[state]))
       return 1;  
    
    return 0;    
}

int foundinFinalStates(t_int_vector array,int value)
{
    int i;
    for(i=0;i<array.dim;i++)
    {
      if (array.vec[i]==value)
         return 1;
    }  
    return 0;
}

